home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 414 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  53 lines

  1. Path: news.nic.surfnet.nl!sun4nl!ittpub!ittpub!nntp
  2. Newsgroups: comp.lang.c++
  3. Subject: Re: Question about destructor...
  4. Message-ID: <1996Jan4.134423.1728@ittpub>
  5. From: wil@ittpub.nl (Wil Evers)
  6. Date: 4 Jan 96 13:44:22 WET
  7. References: <4ces35$3iq$1@mhafn.production.compuserve.com>
  8. Distribution: world
  9. Nntp-Posting-Host: lintilla
  10.  
  11. In article <4ces35$3iq$1@mhafn.production.compuserve.com> Nil Bannerjee  
  12. <100704.1417@CompuServe.COM> writes:
  13.  
  14. [previous discussion: what should derived class CB's destructor do to make  
  15. sure base class CA's destructor is called?]
  16.  
  17. >    If you call the destructor of A from B's destructor then this 
  18. > will take care of the inherited attributes.
  19. > CB::~CB()
  20. > {
  21. >     //your stuff..
  22. >     ..
  23. >     ..
  24. >     ~CA();
  25. > }
  26.  
  27. Sorry, but this is wrong! If you manage to explicitly call the base class  
  28. destructor from the derived class destructor, the base class destructor  
  29. will be called twice, because the base class destructor is called  
  30. automatically after running the derived class destructor code. So in all  
  31. but the most akward cases, a derived class destructor should not try to  
  32. call the base class destructor.
  33.  
  34. Apart from that, the code above should not compile. If you ever need to  
  35. explicitly call a destructor, you must explicitly specify the object on  
  36. which it is to be called:
  37.  
  38. class SomeClass {
  39. public :
  40.     virtual ~SomeClass();
  41.     void destruct()
  42.         { this->~SomeClass(); }
  43.             // may call derived destructor first
  44.     void destroy()
  45.         { this->SomeClass::~SomeClass(); }
  46.             // always calls SomeClass::~SomeClass() directly
  47. };
  48.  
  49. - Wil
  50.  
  51.  
  52.